package com.myway.service.ibe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.myway.dto.Result.SearchResult;
import com.myway.dto.domain.Cabin;
import com.myway.dto.domain.FlightCriteria;
import com.myway.dto.domain.Segment;
import com.myway.dto.domain.TotalFlyPrice;
import com.myway.dto.ibe_param.*;
import com.myway.dto.vo.FlyOrderVo;
import com.myway.dto.vo.FlyPassengeVo;
import com.myway.dto.vo.FlySegmentVo;
import com.myway.pojo.FlyOrder;
import com.myway.pojo.FlyPassenge;
import com.myway.service.CountPriceService;
import com.myway.service.ibe.command.*;
import com.travelsky.ebuild.clientapi.tktchange.dm.reshop.SegmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/1/15.
 */
@Controller
@RequestMapping("/ibe")
public class TestController {

    /**
     *
     * C D Y K L M N O Q T
     */
    @Autowired
    private FareCommand fareCommand;

    @Autowired
    private SearchFlightCommand searchFlightCommand;

    @Autowired
    private ReservePNRCommand reservePNRCommand;

    @Autowired
    private FareSeachCommand fareSeachCommand;

    @Autowired
    private RateChangeCommand rateChangeCommand;

    @Autowired
    private FarRuleCommand farRuleCommand;

    @Autowired
    private NewFareCommand newFareCommand;

    @Autowired
    private SearchOneCommand searchOneCommand;

    @Autowired
    private PNRCommand pnrCommand;

    @Autowired
    private CountPriceService countPriceService;



//    @RequestMapping("/test11")
//    @ResponseBody
//    public String test11() throws Exception{
//        FlightCriteria criteria = new FlightCriteria();
//       // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        criteria.setFlightDate("2018-08-16");
//        criteria.setArrDate("2018-08-16");
//        criteria.setAirline("MJ");
//        criteria.setOrgCity("TBS");
//        criteria.setDstCity("TLV");
//        criteria.setTravlType("ow");
//
//        String travlType = criteria.getTravlType();
//        List<SegmentParam> segments = Lists.newArrayList();
//        //航段1（去程）
//        SegmentParam segmentParamOne = new SegmentParam();
//        //设置起飞时间
//        String strDate = criteria.getFlightDate();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date depDate = sdf.parse(strDate);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(depDate);
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH)+1;
//        System.out.print("---getPrice---"+month);
//        int day = calendar.get(Calendar.DATE);
//        DepatureDateParam depatureDateParam = new DepatureDateParam();
//        depatureDateParam.setYear(year);
//        depatureDateParam.setMonth(month);
//        depatureDateParam.setDay(day);
//        segmentParamOne.setDepartureDate(depatureDateParam);
//        //设置出发地
//        List<Origin> origins = Lists.newArrayList();
//        Origin origin = new Origin();
//        origin.setName(criteria.getOrgCity());
//        origins.add(origin);
//        segmentParamOne.setOrigin(origins);
//        //设置目的地
//        List<Destination> destinations = Lists.newArrayList();
//        Destination destination = new Destination();
//        destination.setName(criteria.getDstCity());
//        destinations.add(destination);
//        segmentParamOne.setDestination(destinations);
//        segments.add(segmentParamOne);
//        if(travlType.equals("rt")){
//            //返程
//            SegmentParam segmentParamTwo = new SegmentParam();
//            //设置返程起飞时间
//            String strArrDate = criteria.getArrDate();
//            Date arrDate = sdf.parse(strArrDate);
//            calendar.setTime(arrDate);
//            int yearArr = calendar.get(Calendar.YEAR);
//            int monthArr = calendar.get(Calendar.MONTH)+1;
//            System.out.print("---getPrice---"+monthArr);
//            int dayArr = calendar.get(Calendar.DATE);
//            DepatureDateParam depatureDateParamTwo = new DepatureDateParam();
//            depatureDateParamTwo.setYear(yearArr);
//            depatureDateParamTwo.setMonth(monthArr);
//            depatureDateParamTwo.setDay(dayArr);
//            segmentParamTwo.setDepartureDate(depatureDateParamTwo);
//            //设置出发地
//            List<Origin> originsArr = Lists.newArrayList();
//            Origin originArr = new Origin();
//            originArr.setName(criteria.getDstCity());
//            originsArr.add(originArr);
//            segmentParamTwo.setOrigin(originsArr);
//            //设置目的地
//            List<Destination> destinationsArr = Lists.newArrayList();
//            Destination destinationArr = new Destination();
//            destinationArr.setName(criteria.getOrgCity());
//            destinationsArr.add(destinationArr);
//            segmentParamTwo.setDestination(destinationsArr);
//            segments.add(segmentParamTwo);
//        }
//        String response = null;
//        try {
//            response = searchOneCommand.execute(segments,true);
//            System.out.println(response);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return response;
//    }

    /**
     * 测试searchone接口
     */
//    @RequestMapping("/test8")
//    @ResponseBody
//    public String test8() throws Exception{
//        SearchOneParam param = new SearchOneParam();
//        List<SegmentParam> segments = new ArrayList<SegmentParam>();
//        //日历
//        /*FlexInterval flexInterval = new FlexInterval();
//        flexInterval.setFrom(3);
//        flexInterval.setTo(3);*/
//        ////航段1
//        SegmentParam segmentParam = new SegmentParam();
//       // segmentParam.setFlexInterval(flexInterval);
//        //出发时间
//        DepatureDateParam departureDate = new DepatureDateParam();
//        departureDate.setYear(2018);
//        departureDate.setMonth(8);
//        departureDate.setDay(5);
//        //航段添加时间
//        segmentParam.setDepartureDate(departureDate);
//        //出发地
//        List<Origin> origin = new ArrayList<Origin>();
//        Origin origin1 = new Origin();
//        origin1.setName("TBS");
//        origin.add(origin1);
//        //航段添加出发地
//        segmentParam.setOrigin(origin);
//        //目的地
//        List<Destination> destination = new ArrayList<Destination>();
//        Destination destination1 = new Destination();
//        destination1.setName("TLV");
//        destination.add(destination1);
//        //航段添加目的地
//        segmentParam.setDestination(destination);
//        //将航段添加到航段集合
//        segments.add(segmentParam);
//
//        ////航段2
//        SegmentParam segmentParam2 = new SegmentParam();
//       // segmentParam2.setFlexInterval(flexInterval);
//        //出发时间
//        DepatureDateParam departureDate2 = new DepatureDateParam();
//        departureDate2.setYear(2018);
//        departureDate2.setMonth(8);
//        departureDate2.setDay(12);
//        //航段添加时间
//        segmentParam2.setDepartureDate(departureDate2);
//        //出发地
//        List<Origin> origins2 = new ArrayList<Origin>();
//        Origin origin2 = new Origin();
//        origin2.setName("TLV");
//        origins2.add(origin2);
//        //航段添加出发地
//        segmentParam2.setOrigin(origins2);
//        //目的地
//        List<Destination> destinations2 = new ArrayList<Destination>();
//        Destination destination2 = new Destination();
//        destination2.setName("TBS");
//        destinations2.add(destination2);
//        //航段添加目的地
//        segmentParam2.setDestination(destinations2);
//        //将航段添加到航段集合
//       // segments.add(segmentParam2);
//
//        String response = searchOneCommand.execute(segments, false);
//        SearchResult criteria = JSON.parseObject(response,new TypeReference<SearchResult>(){});
//        return response;
//
//    }
    /**
     * 测试searchone接口
     */
//    @RequestMapping(value="/test7",produces="application/json")
//    @ResponseBody
//    public String test7() throws Exception{
//
//        SearchOneParam param = new SearchOneParam();
//        List<SegmentParam> segments = new ArrayList<SegmentParam>();
//        //航段
//        SegmentParam segmentParam = new SegmentParam();
//        //出发时间
//        DepatureDateParam departureDate = new DepatureDateParam();
//        departureDate.setYear(2018);
//        departureDate.setMonth(8);
//        departureDate.setDay(16);
//        //航段添加时间
//        segmentParam.setDepartureDate(departureDate);
//        //出发地
//        List<Origin> origin = new ArrayList<Origin>();
//        Origin origin1 = new Origin();
//        origin1.setName("TBS");
//        origin.add(origin1);
//        //目的地
//        List<Destination> destination = new ArrayList<Destination>();
//        Destination destination1 = new Destination();
//        destination1.setName("TLV");
//        destination.add(destination1);
//
//        //航段添加目的地
//        segmentParam.setDestination(destination);
//        //航段添加出发地
//        segmentParam.setOrigin(origin);
//        //将航段添加到航段集合
//        segments.add(segmentParam);
//        //将航段集合添加到searchone属性
//        param.setSegments(segments);
//        //添加货币代码
//        //param.setDisplayCurrency("EUR");
//        //代理信息
//        List<AgenciesParam> agencies = new ArrayList<AgenciesParam>();
//        AgenciesParam agency = new AgenciesParam();
//        agencies.add(agency);
//        //将代理信息添加到searchone
//        param.setAgencies(agencies);
//        //旅客信息
//        List<PassengersParam> passengers = new ArrayList<PassengersParam>();
//        PassengersParam passenger = new PassengersParam();
//        String[] ptc =new String[1];
//        ptc[0]="ADT";
//        passenger.setPtc(ptc);
//        passenger.setVirtual(false);
//        passengers.add(passenger);
//        param.setPassengers(passengers);
//        //行程偏好
//        JourneyPreferencesParam journeyPreferencesParam = new JourneyPreferencesParam();
//        String[] fareTypes = new String[2];
//        fareTypes[0] = "PUBLIC";
//        fareTypes[1] = "PRIVATE";
//        journeyPreferencesParam.setFareTypes(fareTypes);
//        param.setJourneyPreferences(journeyPreferencesParam);
//        //旅客偏好信息
//        PreferencesParam preferencesParam = new PreferencesParam();
//        //设置舱位等级
//        CabinsParam cabinsParam = new CabinsParam();
//        String[] codes = new String[3];
//        codes[0] = "BUSINESS";
//        codes[1] = "ECONOMY";
//        codes[2] = "FIRST";
//        cabinsParam.setCodes(codes);
//        String[] faresAllowedCarriers = new String[1];
//        faresAllowedCarriers[0]="MJ";
//
//        preferencesParam.setFaresAllowedCarriers(faresAllowedCarriers);
//        cabinsParam.setVirtual(false);
//        preferencesParam.setCabins(cabinsParam);
//        param.setPreferences(preferencesParam);
//
//        String response = searchOneCommand.execute(segments,true);
//        SearchResult criteria = JSON.parseObject(response,new TypeReference<SearchResult>(){});
//        return response;
//    }

    /**
     * 测试新运价接口
     */
    @RequestMapping("/test6")
    @ResponseBody
    public String test6() throws Exception{

        String response = newFareCommand.execute();
        return response;
    }


    /**
     * 测试运价规则
     */
    @RequestMapping("/test5")
    @ResponseBody
    public String test5() throws ParseException {
        farRuleCommand.execute();
        return "";
    }

    /**
     * 货币转换查询
     */
    @RequestMapping("/test4")
    @ResponseBody
    public String test4(){
        //rateChangeCommand.execute();
        return "";
    }

    /**
     * 测试航班查询接口
     * @return
     */
//    @RequestMapping("/test")
//    @ResponseBody
//    public String test( ){
//        FlightCriteria flightCriteria = new FlightCriteria();
//        flightCriteria.setDirect(false);
//        flightCriteria.setAirline("MJ");
//        flightCriteria.setOrgCity("BUS");
//        flightCriteria.setDstCity("HRK");
//        flightCriteria.setFlightDate("2018-08-11");
//        flightCriteria.setEticket(true);
//        //Map params = JSON.parseObject(json);
//        //testCommand.execute(new HashMap<>());
//       // FlightCriteria params = JSON.parseObject(json,new TypeReference<FlightCriteria>(){});
//        String jsonResult = searchFlightCommand.execute(flightCriteria);
//        return jsonResult;
//    }
//    @RequestMapping("/test1")
//    @ResponseBody
//    public String test1( ){
//        //TODO:调用接口查询票价
//
//        FlightParam param = new FlightParam();
//        param.setArrDate("2018-08-11T12:25:00");
//        param.setFlightDate("2018-08-11T15:45:00");
//        param.setOrgCity("HRK");
//        param.setDstCity("BUS");
//        param.setEquipment("410");
//        param.setCabinCode("D");
//        param.setCurrency("EUR");
//        param.setTravlType("OW");
//        param.setEquipment("737");
//     /*   FlightParam param2 = new FlightParam();
//        param2.setArrDate("2018-08-15T16:45:00");
//        param2.setFlightDate("2018-08-15T18:15:00");
//        param2.setOrgCity("BUS");
//        param2.setDstCity("HRK");
//        param2.setEquipment("409");
//        param2.setCabinCode("D");
//        param2.setCurrency("EUR");
//        param2.setTravlType("RT");
//        param2.setEquipment("737");*/
//
//        List<FlightParam> params = new ArrayList<FlightParam>();
//        params.add(param);
//        //params.add(param2);
//        TicketPrice ticketPrices =fareCommand.execute(params);
//
//        String json = JSON.toJSONString(ticketPrices);
//        return json;
//    }

    /**
     * 测试pnr预定
     */
//    @RequestMapping("/test2")
//    @ResponseBody
//    public String test2(){
//        Map params = new HashMap();
//        //航段组
//        List<FlySegmentVo> segments = Lists.newArrayList();
//        FlySegmentVo segment = new FlySegmentVo();
//        String Airline="MJ";
//        String FlightNo="MJ301";
//        String Org="TBS";
//        String Dst="TLV";
//        String cabinCode="T";
//        String flightDate="2018-10-22";
//
//        segment.setCurrency("EUR");
//        segment.setTravlType("RT");
//        segment.setFlightNo(FlightNo);
//        segment.setDepCode(Org);
//        segment.setArrCode(Dst);
//
//
//        FlySegmentVo segment1 = new FlySegmentVo();
//        String Airline1="MJ";
//        String FlightNo1="MJ302";
//        String Org1="TLV";
//        String Dst1="TBS";
//        String cabinCode1="T";
//        String flightDate1="2018-10-25";
//
//        segment1.setCurrency("EUR");
//        segment1.setTravlType("RT");
//        segment1.setFlightNo(FlightNo1);
//        segment1.setDepCode(Org1);
//        segment1.setArrCode(Dst1);
//
//        Date date = null ;
//        Date date1 = null;
//        try {
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            date = simpleDateFormat.parse(flightDate);
//            date1 = simpleDateFormat.parse(flightDate1);
//            segment.setDepTime(date);
//            segment1.setDepTime(date1);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        List<Cabin> cabins = Lists.newArrayList();
//        Cabin cabin = new Cabin();
//        cabin.setCabinName(cabinCode);
//        cabins.add(cabin);
//        List<Cabin> cabins1 = Lists.newArrayList();
//        Cabin cabin1= new Cabin();
//        cabin1.setCabinName(cabinCode1);
//        cabins1.add(cabin1);
//
//        segment.setCabinClass(cabinCode);
//        segment1.setCabinClass(cabinCode1);
//        segments.add(segment);
////        segments.add(segment1);
//        //旅客组
//        List<FlyPassengeVo> passenges = Lists.newArrayList();
//        FlyPassengeVo passenge = new FlyPassengeVo();
//        passenge.setName("ZHANG/SAN");
//        passenge.setPassageType("1");
//        passenge.setCardType("NI");
//        passenge.setCardNo("142427198812175414");
//        passenge.setPassgeBirth(new Date());
//        passenges.add(passenge);
//        //联系组
//        String contact = "15123339999";
//
//        FlyOrderVo flyOrder = new FlyOrderVo();
//        flyOrder.setCurrency("EUR");
//        flyOrder.setContactName("ZHANG/SAN");
//        flyOrder.setContactAddress("faf");
//        flyOrder.setContactMoble("123123123");
//
//        /**ticket*/
//        FlightCriteria criteria = new FlightCriteria();
//
//        criteria.setDirect(true);
//        criteria.setAirline("MJ");
//
//        criteria.setOrgCity(Org);
//        criteria.setDstCity(Dst);
//        criteria.setFlightDate(flightDate);
//        criteria.setEticket(true);
//        String executeJson = searchFlightCommand.execute(criteria);
//        List<Segment> list = JSON.parseArray(executeJson,Segment.class);
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date11 = sdf.format(list.get(0).getDepartDate());
//        String date2 = sdf.format(list.get(0).getArriveDate());
//        String[] depDate = date11.split(" ");
//        String[] arrDate = date2.split(" ");
//        criteria.setFlightDate(depDate[0]+"T"+depDate[1]);
//        criteria.setArrDate(arrDate[0]+"T"+arrDate[1]);
//
//        FlightParam param = new FlightParam();
//        param.setArrDate(arrDate[0]+"T"+arrDate[1]);
//        param.setFlightDate(depDate[0]+"T"+depDate[1]);
//        param.setOrgCity(Org);
//        param.setDstCity(Dst);
//        param.setEquipment(FlightNo);
//        param.setCabinCode(cabinCode);
//
//        List<FlightParam> params1 = new ArrayList<FlightParam>();
//        params1.add(param);
//        TicketPrice ticketPrices =fareCommand.execute(params1);
//        Map map=countPriceService.countCabinTypePrice(ticketPrices);
//        TotalFlyPrice totalFlyPrice =(TotalFlyPrice)map.get("totalFlyPrice");
//        List<TaxPrice> taxPrices =ticketPrices.getTaxPrices();
//        params.put("segments",segments);
//        params.put("passenges",passenges);
//        params.put("contact",contact);
//        params.put("order",flyOrder);
//        params.put("ticketPrice",ticketPrices);
//        params.put("totalFlyPrice",totalFlyPrice);
//        params.put("taxPrices",taxPrices);
//        params = reservePNRCommand.execute(params);
//        return JSON.toJSONString(params);
//    }

    @RequestMapping("/test3")
    @ResponseBody
    public String test3() throws  Exception{
        fareSeachCommand.FareSeach();
        return "";
    }
}
